package zzg.study;

import zzg.study.download.Downloader;
import zzg.study.download.FileDownloader;
import zzg.study.download.MultiThreadFileDownloader;
import zzg.study.download.SimpleDownloader;
import zzg.study.support.MultiThreadDownloadProgressPrinter;

import java.awt.*;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * @Classname FastDownloadApplication
 * @Description TODO
 * @Date 2024/2/21 22:06
 * @Author zhuzhenguo
 */
public class FastDownloadApplication {

    public static void main(String[] args) throws IOException {

//        String fileUrl = "http://img.doutula.com/production/uploads/image/2017/10/19/20171019627498_uQtkcl.jpg";
        String fileUrl = "https://dldir1.qq.com/qqfile/qq/PCQQ9.7.1/QQ9.7.1.28924.exe";

        //通过内存下载 下载QQ 87s
//        Downloader downloader = new SimpleDownloader();
//        downloader.download(fileUrl, "C:\\Users\\朱振国\\Desktop\\fastDownload");

        //单线程下载 下载QQ 47s
//        Downloader downloader = new FileDownloader();
//        downloader.download(fileUrl, "C:\\Users\\朱振国\\Desktop\\fastDownload");

        //多线程下载 26s
        MultiThreadDownloadProgressPrinter downloadProgressPrinter = new MultiThreadDownloadProgressPrinter(10);

        CompletableFuture.runAsync(() -> {
            while (true) {
                long alreadyDownloadLength = downloadProgressPrinter.getAlreadyDownloadLength();
                long contentLength = downloadProgressPrinter.getContentLength();
                System.out.println(contentLength + "  =>  " + alreadyDownloadLength);
                if (alreadyDownloadLength != 0 && alreadyDownloadLength > contentLength) {
                    break;
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Downloader downloader = new MultiThreadFileDownloader(10, downloadProgressPrinter);
        downloader.download(fileUrl, "C:\\Users\\朱振国\\Desktop\\fastDownload");

    }

}
